package com.shunda.ysdgj.manager.controller.system;

import com.shunda.ysdgj.manager.service.system.SysMenuService;
import com.shunda.ysdgj.manager.service.system.SysRoleMenuService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.entity.system.SysMenu;
import com.shunda.ysdgj.model.entity.system.SysRoleMenu;
import com.shunda.ysdgj.model.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "系统菜单接口")
@RestController
@RequestMapping("/api/v1/admin/system/menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;
    private final SysRoleMenuService sysRoleMenuService;

    @Operation(summary = "获取所有可用菜单和已分配的菜单ID")
    @GetMapping("assigned/{roleId}")
    public ResultVo assigned(@PathVariable Long roleId) {
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("menus", sysMenuService.findAllAvailable());
        responseData.put("assignedMenus", sysRoleMenuService.getAssignedMenuIds(roleId));
        return ResultVo.success(responseData);
    }

    @Operation(summary = "分配菜单")
    @PostMapping("assign")
    public ResultVo assign(@RequestBody List<SysRoleMenu> sysRoleMenuList) {
        if (!sysRoleMenuService.add(sysRoleMenuList)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "获取所有系统菜单")
    @GetMapping("all")
    public ResultVo all() {
        return ResultVo.success(sysMenuService.findAll());
    }

    @Operation(summary = "添加系统菜单")
    @PostMapping("add")
    public ResultVo add(@RequestBody SysMenu sysMenu) {
        if (!sysMenuService.add(sysMenu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改系统菜单")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody SysMenu sysMenu) {
        if (!sysMenuService.modify(sysMenu)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除系统菜单")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!sysMenuService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
